#coding=utf-8
import json
import os

from utils.httpUtil import http_util

# 微博内容实体
from utils.spiderUtil import spiderUtil


class WeiboContent():
    def __init__(self, weibo_content, weibo_time, weibo_profile_id, comments_count,reposts_count,
                 attitudes_count,author_name,profile_url,author_desc,author_follow,author_fans):
        # 微博内容
        self.weiboContent = weibo_content
        # 微博发布时间 (date)
        self.weiboTime = weibo_time
        # 博主id
        self.weiboProfileId = weibo_profile_id
        # 评论数
        self.commentsCount = comments_count
        # 转发数
        self.repostsCount = reposts_count
        # 点赞数
        self.attitudesCount = attitudes_count
        # 博主昵称
        self.authorName = author_name
        # 博主微博地址
        self.profileUrl = profile_url
        # 博主描述
        self.authorDesc = author_desc
        # 博主关注数
        self.authorFollow = author_follow
        #博主粉丝数
        self.authorFans = author_fans

# 微博爬虫配置
class WeiboConfig():

    def __init__(self):
        # 微博上传服务器地址
        self.uploadMysqlUrl = "http://${WMS_JAVA_HOST}:${SERVER_PORT}/renren-fast/wms/weibocontent/save"
        # 微博图片上传地址
        self.uploadFileUrl = "http://${FASTDFS_CLI_HOST}:${FASTDFS_CLI_PORT}/api/t1/v1/fastdfs-service/upload/netimage"

# 全局单例
weibo_config = WeiboConfig()

# 微博工具
class WeiBoUtil():

    def __init__(self):
        # 微博配置
        self.weibo_config = weibo_config
        # 微博配置项
        # 搞笑板块
        self.weibo_fun_url = "https://m.weibo.cn/api/container/getIndex?containerid=102803_ctg1_4388_-_ctg1_4388&openApp=0"
        # 情感板块
        self.weibo_motion_url = "https://m.weibo.cn/api/container/getIndex?containerid=102803_ctg1_1988_-_ctg1_1988&openApp=0"
        # 爬取类型
        self.weibo_type = ""
        # 微博爬取参数
        # 总爬取数目
        self.total_num = 20
        # 最小点赞数
        self.good_nums = 150

        # 微博中转集合，每次使用前要清空
        # 爬取内容集合
        self.cont_list = []
        # 爬取标题集合
        self.title_list = []

    # 初始化微博工具
    def init_config(self):
        self.cont_list = []
        self.title_list = []

    # 获取每日搞笑板块微博
    def get_daily_funny(self):
        self.weibo_type = "搞笑"
        self.get_weibo_to_cont(self.weibo_fun_url)

    # 获取每日情感板块微博
    def get_daily_motion(self):
        self.weibo_type = "情感"
        self.get_weibo_to_cont(self.weibo_motion_url)

    # 微博内容爬取
    def get_weibo_to_cont(self, weibo_url):
        data = spiderUtil.use_proxy(weibo_url)
        content = json.loads(data).get('data')
        cards = content.get('cards')
        if (self.total_num is None or self.total_num == 0):
            self.total_num = len(cards)
        if (len(cards) == 0):
            return
        # 有效数目
        for j in range(len(cards)):
            # 已经达到有效数目，成功
            if (len(self.title_list) == self.total_num):
                return True
            # 如果已经到页面的最后一条，则做翻页处理
            if (j == len(cards) - 1):
                return False

            # print("-----正在爬取第" + str(page) + "页，第" + str(j) + "条微博------")
            card_type = cards[j].get('card_type')
            if (card_type == 9):
                mblog = cards[j].get('mblog')
                # 点赞数
                attitudes_count = mblog.get('attitudes_count')
                # 评论数
                comments_count = mblog.get('comments_count')
                # 发布时间
                created_at = mblog.get('created_at')
                # 转发数
                reposts_count = mblog.get('reposts_count')
                # 微博地址
                scheme = cards[j].get('scheme')
                text = mblog.get('text')

                weibo_type = self.weibo_type;

                # 保存微博到mysql
                weibo_cont = WeiboContent(text, created_at, None, comments_count, reposts_count, attitudes_count, None
                                          , scheme, weibo_type, None, None)
                res = weibo_util.post_weibo(weibo_cont)
                res_code = res["code"]
                if res_code == 0:
                    # 获取微博id
                    weibo_id = res["data"]
                else:
                    continue

                # V1.0.1修改
                # 微博只有点赞数 > 一定数目才作为有效数据
                # if(attitudes_count < goodNums):
                #      continue

                # 网络图片组：
                pic_list = []
                payload = {}
                # 初始化
                payload.update({"weiboId": weibo_id})

                # 微博图片爬取
                pics = mblog.get('pics')
                if pics is not None and len(pics) > 0:
                    for index in range(0, len(pics)):
                        pic = pics[index]
                        picurl = pic.get('url')
                        print('pic:' + picurl)
                        # 暂不支持动图，以后只能取指定大小内的图片0
                        # 不爬取动图 todo
                        picurl = str(picurl).replace("orj360", "large")
                        pic_list.append(picurl)
                        #
                        # picDiv = "<div class='pic-margin'><img src=" + picurl + "/></div>"
                        # finalPicDiv += picDiv
                else:
                    # 直到找到有图的博文为止
                    continue

                # 系列组图
                # self.contlist.append(finalPicDiv)
                # 系列标题
                # self.titlelist.append(text)

                if len(pic_list) != 0:
                    # 上传图片至文件服务器
                    payload.update({"urlSrcList": pic_list})
                    http_util.post_form_data(weibo_config.uploadFileUrl, payload)

    # 保存图片到mysql
    def post_weibo(self, weibo_content):
        if weibo_config.uploadMysqlUrl:
            # json_param = object_util.object2json(weibo_content)
            res = http_util.post(weibo_config.uploadMysqlUrl, weibo_content.__dict__, None)
            return res
        else:
            return

    # 上传图片至文件服务器
    def post_picture(self, data, pic_url):
        if self.weibo_config.uploadFileUrl:
            res_dict = http_util.get_pic_content(pic_url)
            file_path = res_dict['file_path']
            files = {'file': open(file_path, 'rb')}
            res = http_util.post_file(self.weibo_config.uploadFileUrl, data, files)
            if res['code'] == 200:
                # 删除file
                os.remove(file_path)
# 模块单例
weibo_util = WeiBoUtil()
